/* 14. 超期读者列表 */
"use strict";

const CHECK = require("../myUtil/formatCheck");
const { StrTime } = require("../myUtil/lib");
const DATEUTIL = require("../myUtil/DateUtil");
const db = require("../../coSqlite3");
const HTM = require("../myUtil/lib").html;

exports.queryOverDateUser = function* (req, res) {
  console.log("----------------------14. 超期读者列表\n");
  let htm = "";
  let rows = "";
  let date = StrTime(new Date(), "yyyy-mm-dd");
  try {
    // let exists = yield db.execSQL(
    //   "SELECT DISTINCT rid FROM process WHERE returned = 0 and return_time < ? ",
    //   [date]
    // );
    // if (exists.length == 0) {
    //   return HTM.head + HTM.tableBegin + "" + HTM.tableEnd + HTM.end;
    // }
    // let args = [];
    // for (const item of exists) {
    //   args.push(item.rid);
    // }
    // rows = yield db.execSQL("SELECT * FROM reader WHERE rid in '('||?||')' ", args);
    rows = yield db.execSQL("select * from reader where rid in (select distinct rid from process where returned = 0 and return_time < ?)", [date]);
  } catch (err) {
    htm = HTM.tableBegin + HTM.tableEnd + "数据库执行失败: ";
    return HTM.head + htm + err + HTM.end;
  }
  for (let row of rows) {
    htm +=
      "<tr><td>" +
      row.rid +
      "</td><td>" +
      row.rname +
      "</td><td>" +
      row.rsex +
      "</td><td>" +
      row.rdept +
      "</td><td>" +
      row.rgrade;
    ("</td></tr>\n");
  }
  return HTM.head + HTM.tableBegin + htm + HTM.tableEnd + HTM.end;
};
